package com.minyifang.wcspf.common.widgets;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.minyifang.wcspf.common.utils.LogUtil;

/**
 * Created by xuyougen on 2018/6/5.
 */

public class CountdownCircleView extends View {
    Paint mPaint;
    private int mWidth;
    private int mHeight;
    private int mStrokeWidth;
    float mProgress = 1f;
    private int mBgColor = Color.parseColor("#55000000");
    OnTimeChangeListener mOnTimeChangeListener;

    RectF mRectF = new RectF();
    RectF mRectF2 = new RectF();

    public CountdownCircleView(Context context) {
        this(context, null);
    }

    public CountdownCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountdownCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    int sec = 0;

    private void init(Context context) {
        mStrokeWidth = (int) (context.getResources().getDisplayMetrics().density * 2);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(mStrokeWidth);

//        ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
//        animator.setDuration(3*1000);
//        animator.setRepeatCount(ValueAnimator.INFINITE);
//        animator.setInterpolator(new LinearInterpolator());
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                mProgress = (float) animation.getAnimatedValue();
//                invalidate();
//            }
//        });
//        animator.start();
    }

    ValueAnimator animator;

    public void start(int seconds, OnTimeChangeListener listener) {
        mOnTimeChangeListener = listener;
        sec = seconds;
        animator = ValueAnimator.ofFloat(1, 0);
        animator.setDuration(sec * 1000);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mProgress = (float) animation.getAnimatedValue();
//                int t = (int) (mProgress * sec);
//                if (t != sec) {
//                    sec = t;
//                    LogUtil.i(sec+"");
//                    if (mOnTimeChangeListener!=null){
//                        mOnTimeChangeListener.onTimeChange(sec);
//                    }
//                }
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (mOnTimeChangeListener != null) {
                    mOnTimeChangeListener.onTimeOver();
                }
            }
        });
        animator.start();
    }

    public void stopCircleAnimation() {
        mOnTimeChangeListener = null;
//        if (animator != null && animator.isRunning()) {
//            animator.end();
//        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        int offset = mStrokeWidth / 2;
        mRectF.set(offset, offset, w - offset, h - offset);
        mRectF2.set(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mBgColor);
        canvas.drawArc(mRectF2, 0, 360, false, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(mRectF, 0, 360, false, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        canvas.drawArc(mRectF, -90, 360 * mProgress, false, mPaint);
    }

    public interface OnTimeChangeListener {
        void onTimeOver();

//        void onTimeChange(int sec);
    }
}
